package com.ckf.login_wx.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.Disburse;
import com.ckf.login_wx.entity.Material;
import com.ckf.login_wx.entity.Repertory;
import com.ckf.login_wx.entity.Sell;
import com.ckf.login_wx.entity.Xinsell;
import com.ckf.login_wx.servic.DisburseService;
import com.ckf.login_wx.util.Idname;
import com.github.pagehelper.PageHelper;

@RestController
@RequestMapping("/Sp")
public class SpDisbursecontroller {
	@Autowired
    private DisburseService disburseService;
    @PostMapping("/addordex")
    public Map<String, Object> addordex(int id,String purpose,String time,String remark,String faf,String money) throws ParseException{
        Map<String, Object> map = new HashMap<>();
        Disburse disburse=new Disburse();
        disburse.setMoney(Double.parseDouble(money));
        disburse.setPurpose(purpose);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        disburse.setTime(simpleDateFormat.parse(time));
        disburse.setRemark(remark);
        if(faf.equals("add")){
	        if (disburseService.save(disburse)){
	            map.put("result","添加成功");
	        }else {
	            map.put("result","添加失败");
	        }
	        return map;
		}
		if(faf.equals("up")){
			disburse.setId(id);
	        if (disburseService.updateById(disburse)){
	            map.put("result","修改成功");
	        }else {
	            map.put("result","修改失败");
	        }
		}
        return map;
    }
    @GetMapping("/deledisbursebyid")
    public void deledisbursebyid(Integer id){
    	disburseService.removeById(id);
    }
    @RequestMapping("/deledisburseall")
    public Object deledisburseall(String[] id){
    	
    	for (int i = 0; i < id.length; i++) {
    		try {
    			if(id[i].equals(""))continue;
    	    	int idint = Integer.parseInt(id[i]+"");
    	    	disburseService.removeById(idint);
			} catch (Exception e) {
				System.out.println("***********************************异常*************************************");
				return "异常";
			}
		}
		return "删除成功";
    }
    @GetMapping("/orddisbursebysearch")
    public Object orddisbursebysearch(String search,String talk,String pagenew){//
        Integer i = Integer.parseInt(pagenew);
    	if(search.equals("时间")){
       	 	PageHelper.offsetPage((i-1)*5, 5);
    		return disburseService.seletime(talk);
    	}
//        QueryWrapper<Disburse> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return disburseService.selectlike(Idname.getname(search), talk);
    }
    @GetMapping("/orddisbursebysearchall")
    public Object orddisbursebysearchall(String search,String talk){//
    	if(search.equals("时间")){
    		return disburseService.seletime(talk);
    	}
//        QueryWrapper<Disburse> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
        return disburseService.selectlike(Idname.getname(search), talk);
    }
    @PostMapping("/seledisbursebyid")
    public Object seledisbursebyid(String id){
    	Integer idint = Integer.parseInt(id);
        return disburseService.getById(idint);
    }
    @PostMapping("/updisbursebyid")
    public Object updisbursebyid(String id,String purpose,String time,String remark,String money) throws ParseException{
        Map<String, Object> map = new HashMap<>();
        Integer i = Integer.parseInt(id);
    	Disburse disburse=new Disburse();
    	disburse.setId(i);
        disburse.setMoney(Double.parseDouble(money));
        disburse.setPurpose(purpose);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        disburse.setTime(simpleDateFormat.parse(time));
        disburse.setRemark(remark);
		if (disburseService.updateById(disburse)){
            map.put("code",200);
            map.put("result","修改成功");
            System.out.println("修改成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }

    @RequestMapping("/exlistall")
    public Object exlistall(HttpServletRequest request){
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
    	
        
    	Map<String, Object> map = new HashMap<>();
    	List<Disburse> a= disburseService.list();
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",disburseService.list());
        return map;
    }
    @RequestMapping("/exlistsearch")
    public Object exlistsearch(HttpServletRequest request,String purpose,String remark,String time){
    	String pageNo=request.getParameter("page");
		String pagesize=request.getParameter("limit");
    	Integer i = Integer.parseInt(pageNo); 
    	Integer n = Integer.parseInt(pagesize); 
    	
    	QueryWrapper<Disburse> queryWrapper = new QueryWrapper<>();
        if(time!=null&&!time.equals("")&&time.length()>13&&time.indexOf("-")!=-1){
            String timestart=time.substring(0,10);
            String timeend=time.substring(12);
            queryWrapper.le("time", timeend);
            queryWrapper.ge("time", timestart);
        }
        queryWrapper.like("purpose", purpose).like("remark", remark);
        
    	Map<String, Object> map = new HashMap<>();
    	List<Disburse> a= disburseService.list(queryWrapper);
    	map.put("code",0);
        map.put("count",a.size());
        map.put("msg"," ");
    	if(n!=5000)PageHelper.offsetPage((i-1)*n, n);
        map.put("data",disburseService.list(queryWrapper));
        return map;
    }
    @RequestMapping("/extotalInfo")
    public Object extotalInfo(String purpose,String time,String remark){
    	QueryWrapper<Disburse> queryWrapper = new QueryWrapper<>();

        queryWrapper.like("purpose", purpose)
		.like("remark", remark);
        if(time!=null&&!time.equals("")&&time.length()>13&&time.indexOf("-")!=-1){
            String timestart=time.substring(0,10);
            String timeend=time.substring(12);
            queryWrapper.le("time", timeend);
            queryWrapper.ge("time", timestart);
        }
    	double total=0;int quantite=0;
    	List<Disburse> mate=disburseService.list(queryWrapper);
    	for (int i = 0; i < mate.size(); i++) {
			total+=mate.get(i).getMoney();
			//quantite+=mate.get(i).getQuantite();
			
		}
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        //map.put("quantite",quantite);  
        return map;
    }
    @RequestMapping("/extotal")
    public Object extotal(){
    	double total=0;int quantite=0;
    	List<Disburse> mate=disburseService.list();
    	for (int i = 0; i < mate.size(); i++) {
			total+=mate.get(i).getMoney();
			//quantite+=mate.get(i).getQuantite();
		}
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        //map.put("quantite",quantite);  
        return map;
    }
}
